﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilLib;
using ServerBase.Protocol;
using ServerBase.Config;
using System.Collections.Generic;
using static ServerBase.Service.BaseDispatch;
using static ServerBase.Config.Conf;
using UtilPublic.ServerLoger;
using UtilPublic.Security;
using ServerGame.Service;
using ServerBase.Other;
using System;
using System.Threading;
using ServerBase.Service;
using static ServerGame.Service.Dispatcher;
using ServerBase.Client;
using static UtilPublic.Redis.ServerRedis;
using static ServerBase.DataBaseRedis.RdsTxt;
using static UtilPublic.Serialization.Jsh;
using ServerBase.IniFile;
using System.Text;
using static ServerBase.IniFile.IniConf;
using ServerPublic.Manager;
using ServerGame.GameObject;
using System.Collections.Concurrent;
using ServerPublic.DataBaseMysql;
using UtilPublic.ApiDispatcher;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using UtilPublic.Serialization;

namespace ServerGame.Manager
{
    
    /// <summary>
    /// 游戏服务器GM管理器
    /// </summary>
    public static partial class GmManager
    {
        //绑定处理
        public static void RegisterGmEventHandler()
        {
            DispatchApiRegister(GmDispatcherMgr.Instance);
        }

        public static void DispatchApiRegister<T>(T t)
        {
            try
            {
                var dict = ApiDpGmMgr.GetController<T, EProtocolResult>(t);
                foreach (var kvp in dict)
                {
                    DictGmEvent[((EGtAction)(kvp.Key)).ToString()] = kvp.Value;
                }
            }
            catch (Exception ex)
            {
                loger.Error($"GmManager DispatchApiRegister", ex);
            }
        }
        //绑定列表
        public static Dictionary<string, Func<string, (EProtocolResult, string)>> DictGmEvent = new Dictionary<string, Func<string, (EProtocolResult, string)>>();

        public static string HandlerGm(HttpRequest req, string Body)
        {
            return ProcessHttpGmReq(req, Body);
        }
        public static string ProcessHttpGmReq(HttpRequest req, string Body)
        {
            var Rsp = new CLS_WebGm()
            {
            };
            var result = EProtocolResult.返回管理员数据错误;
            try
            {
                var Req = Jsh.Jobj<CLS_WebGm>(Body);
                Rsp.GtAction = Req.GtAction;
                var GtAction = Req.GtAction;
                var ReqGtArgs = Req.GtArgs;
                loger.Debug($"ProcessHttpGmReq {Jsh.Jstr(Req)}");
                if (DictGmEvent.ContainsKey(Req.GtAction))
                {
                    try
                    {
                        if (BaseServerInfo.CfgPlatform.ListGm.Contains(ThisSession.Account))
                        {
                            result = EProtocolResult.成功;
                            if (GmManager.DictGmEvent.TryGetValue(GtAction, out var act))
                            {
                                (result, Rsp.GtArgs) = act(Req.GtArgs);

                                if (Req.GtAction == EGtAction.上传配置文件.ToString())
                                {
                                    (ERecordType.管理员操作, EActionSource.系统).Rec(0, Req.Account, Req.GtAction);
                                }
                                else
                                {
                                    (ERecordType.管理员操作, EActionSource.系统).Rec(0, Req.Account, Req.GtAction, Req.GtArgs);
                                }
                            }
                            else
                            {
                                result = EProtocolResult.错误的修改类型;
                            }
                        }
                        else
                        {
                            result = EProtocolResult.管理员登陆账号错误;
                        }
                        if (!result.Success())
                        {
                            Rsp.Account = result.ToString();
                        }
                    }
                    catch (Exception e)
                    {
                        loger.Warn($"处理指令：{GtAction}  出错", e);
                        result = EProtocolResult.错误的修改类型;
                    }
                }
                else
                {
                    loger.Warn($"无效GM指令：{GtAction}");
                    result = EProtocolResult.错误的修改类型;
                }
            }
            catch
            {
                loger.Warn($"反序列化请求错误");
                result = EProtocolResult.管理员登陆账号错误;
            }
            if (!result.Success())
            {
                Rsp.Account = result.ToString();
            }
            var resultSend = Jsh.Jstr(Rsp);
            loger.Debug($"ProcessHttpGmReq {resultSend}");
            return resultSend;
        }
    }
    public partial class DispatcherMsg
    {
        [ApiDpPro((short)EProtocolId.B2G_GT_ACTION, "处理GM功能")]
        public short OnGtAction()
        {
            (var Req, var Rsp) = ThisReq.Body.OnGtAction();

            loger.Debug($"B2G_Gt_Action {Jsh.Jstr(Req)}");
            Rsp.Pin = Req.Pin;
            Rsp.GtAction = Req.GtAction;
            if (!GmManager.检测账号(Req, Rsp))
            {
                return 1;
            }
            try
            {
                var GtAction = Req.GtAction;
                if (GmManager.DictGmEvent.TryGetValue(GtAction, out var act))
                {
                    (Rsp.Result, Rsp.GtArgs) = act(Req.GtArgs);
                    Send(Rsp);

                    loger.Debug($"G2B_Gt_Action {Jsh.Jstr(Rsp)}");

                    if (Req.GtAction == EGtAction.上传配置文件.ToString())
                    {
                        (ERecordType.管理员操作, EActionSource.系统).Rec(0, ThisSession.Account, Req.GtAction);
                    }
                    else
                    {
                        (ERecordType.管理员操作, EActionSource.系统).Rec(0, ThisSession.Account, Req.GtAction, Req.GtArgs);
                    }
                }
            }
            catch
            {
                Rsp.Result = EProtocolResult.错误的修改类型; return Send(Rsp);
            }
            return 0;
        }
    }
}